<template>
    <div class="box">
        <div class="from">
            <el-form ref="ruleFormRef" style="max-width: 600px" :model="ruleForm" :rules="rules" label-width="auto">
                <el-form-item label="用户名" prop="userName">
                    <el-input v-model="ruleForm.userName" />
                </el-form-item>
                <el-form-item label="密码" prop="passWord">
                    <el-input v-model="ruleForm.passWord" type="password" />
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="submitForm(ruleFormRef)">
                        登录
                    </el-button>

                </el-form-item>
            </el-form>
        </div>
    </div>
</template>

<script setup>
import { reactive, ref } from 'vue'
import { loginApi } from "@/api/api"
     import { ElMessage } from 'element-plus'
import { useRoute, useRouter } from 'vue-router'
const router = useRouter()
const ruleFormRef = ref()
const ruleForm = reactive({
    userName: '',

    passWord: '',
})

const rules = reactive({
    userName: [
        { required: true, message: '请输入用户名', trigger: 'blur' },
        { min: 3, max: 5, message: '请输入3-5', trigger: 'blur' },
    ],
    passWord: [
        { required: true, message: '请输入密码', trigger: 'blur' },
        { min: 3, max: 8, message: '请输入3-8', trigger: 'blur' },
    ],
})

const submitForm = async (formEl) =>{
    if (!formEl) return
    await formEl.validate((valid, fields) => {
        if (valid) {
            console.log('submit!')

            loginApi(ruleForm.userName,ruleForm.passWord).then((res)=>{
                 console.log(res);
                if (res.data.code==1) {
                    ElMessage({
                        message: '登陆成功',
                        type: 'success',
                    })
                    localStorage.setItem("token",res.data.token)
                    router.push('/list/index')
                }else{
                     ElMessage.error(res.data.message)
                }
            })
        } else {
            console.log('error submit!', fields)
        }
    })
}

const resetForm = (formEl) => {
    if (!formEl) return
    formEl.resetFields()
}
</script>

<style lang="scss" scoped>
.box {
    width: 100%;
    height: 100%;
    background-color: rgb(49, 135, 211);
    display: flex;
    align-items: center;
    justify-content: flex-end;

    .from {
        width: 400px;
        height: 400px;
        background-color: aliceblue;

    }
}
</style>